package tp.pr3.instructions;

import java.util.StringTokenizer;

import tp.pr3.NavigationModule;
import tp.pr3.RobotEngine;
import tp.pr3.instructions.exceptions.InstructionExecutionException;
import tp.pr3.instructions.exceptions.WrongInstructionFormatException;
import tp.pr3.items.ItemContainer;

public class DropInstruction
extends java.lang.Object
implements Instruction {
    private NavigationModule navigation;
    private ItemContainer robotContainer;
    private  String itemDropId;
    
    public DropInstruction()
    {
       	navigation=null;
    	robotContainer=null;
    	itemDropId=null;
    }
    
	public Instruction parse(java.lang.String cad)throws WrongInstructionFormatException
	{
		StringTokenizer instructionToken = new StringTokenizer(cad);
		if (instructionToken.countTokens() != 2)
			throw new WrongInstructionFormatException();
		
		String instructionAction=instructionToken.nextToken();
		if (instructionAction.equalsIgnoreCase("DROP") || instructionAction.equalsIgnoreCase("SOLTAR"))
			{
			itemDropId=instructionToken.nextToken();
			return this;
			}
		throw new WrongInstructionFormatException();
	}
	
	public void execute() throws InstructionExecutionException
	{
		
		if(!robotContainer.containsItem(itemDropId)) 
			throw new InstructionExecutionException("You do not have any "
					+ itemDropId + ".\n");
		else if(navigation.findItemAtCurrentPlace(itemDropId))
			throw new InstructionExecutionException("Este sitio ya contiene " + itemDropId +"\n");
		navigation.dropItemAtCurrentPlace(robotContainer.pickItem(itemDropId));	
		System.out.println("Great! I have dropped " + itemDropId);
	}
	
	public java.lang.String getHelp()
	{
		return ("DROP | SOLTAR <id>");
	}
	
	public void configureContext(RobotEngine engine,
            NavigationModule navigation,
            ItemContainer robotContainer)
	{
		this.navigation=navigation;
		this.robotContainer=robotContainer;
	}
	
}
